package com.se.Login;

import com.se.common.MessageType;
import com.se.common.User;
import com.se.utils.ClientUtils;
import com.se.utils.ServiceUtils;
import com.se.utils.StageManager;
import com.se.utils.StageType;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Cursor;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * 登录按钮点击事件处理（JavaFX控制器方法）
 */
public class LoginController implements Initializable {
    public Button btnLogin;
    //public Button btnRegisterJump;
    @FXML
    public Button btnClose;
    @FXML
    public Button btnMin;
    public PasswordField pwfLogin;
    public TextField tfLogin;
    @FXML
    private Hyperlink linkRegister;

    @FXML
    protected void btnCloseClick() {
        StageManager.get(StageType.LOGIN_STAGE).close();
    }

    @FXML
    protected void btnLoginClick() {
        LoginService ls = new LoginService();
        btnLogin.setDefaultButton(true);//实现enter登录
        if(tfLogin.getText().isEmpty()) {
            ClientUtils.information_Alert("用户名不能为空！");
            return;
        }

        if(pwfLogin.getText().isEmpty()) {
            ClientUtils.information_Alert("密码不能为空！");
            return;
        }

        try {
            String status = ls.checkUserLogin(tfLogin.getText(), pwfLogin.getText());
            if(MessageType.MESSAGE_LOGIN_SUCCESS.equals(status)){
                ClientUtils.information_Alert("登录成功");

                ServiceUtils.setSpeaker(new User(tfLogin.getText(), pwfLogin.getText()));
                ClientUtils.showMainStage();
                StageManager.get(StageType.LOGIN_STAGE).close();
                StageManager.remove(StageType.LOGIN_STAGE);
                StageManager.remove(StageType.REGISTER_STAGE);
            }
            else if(MessageType.MESSAGE_LOGIN_FAILURE_PASSWORD_NOT_CORRECT.equals(status)) {
                ClientUtils.information_Alert("密码不正确！");
            }
            else if(MessageType.MESSAGE_LOGIN_FAILURE_USER_NOT_EXIST.equals(status)) {
                ClientUtils.information_Alert("用户不存在！");
            }
            else if (MessageType.MESSAGE_CLIENT_REQUEST_ERROR.equals(status)) {
                ClientUtils.information_Alert("您已经登录！");
            }
            else if(MessageType.MESSAGE_SERVER_ERROR.equals(status)) {
                ClientUtils.information_Alert("服务器异常！");
            }
            else {
                ClientUtils.information_Alert("未知错误！");
            }

            /*
            异常处理待优化
            当前只是简单捕获并抛出运行时异常，正式环境需要更友好的错误提示
             */
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @FXML
    protected void btnRegisterJumpClick() {
        Stage loginStage = StageManager.get(StageType.LOGIN_STAGE);
        Stage formerStage = StageManager.get(StageType.REGISTER_STAGE);
        if(loginStage != null) {
            loginStage.hide();
        }

        if(formerStage != null) {
            formerStage.show();
            return;
        }

        ClientUtils.showRegisterStage();
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        //btnClose.setOnMouseEntered(e -> btnClose.setCursor(Cursor.HAND));
        //btnClose.setOnMouseExited(e -> btnClose.setCursor(Cursor.DEFAULT));
    }

    @FXML
    public void btnCloseMouseEntered(MouseEvent mouseEvent) {
        btnClose.setStyle("-fx-background-color: red; -fx-cursor: hand;");
    }
    @FXML
    private void btnCloseMouseExited() {
        btnClose.setStyle("-fx-background-color: transparent; -fx-cursor: hand;");
    }


    public void btnMinimizeMouseEntered(MouseEvent mouseEvent) {
        btnMin.setStyle("-fx-background-color: grey; -fx-cursor: hand;");
    }

    public void btnMinimizeMouseExited(MouseEvent mouseEvent) {
        btnMin.setStyle("-fx-background-color: transparent; -fx-cursor: hand;");
    }

    public void btnMinimizeClick(ActionEvent actionEvent) {
        Stage stage = (Stage) btnMin.getScene().getWindow();
        stage.setIconified(true);
    }
}
